<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use App\Models\Withdraw;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class CloundPayController extends Controller
{
    public function bankNotify(Request $request)
    {
        Log::info($request->all());
        if ($request->code == '40011') {
            $withdraw = Withdraw::where('order_no', $request->data->order_no)->first();
            if (isset($withdraw) && $withdraw->status == 0) {
                $withdraw->order_id = $request->data->order_id;
                $withdraw->payment_no = $request->data->payment_no;
                $withdraw->status = 1;
                $withdraw->save();
            }
        } else {
            Log::info($request->all());
        }
    }

    public function bindWechat(Request $request)
    {
        $token = $request->token;
        $userId = Cache::get($token);
        if (!$userId) {
            return '二维码过期';
        }
        Cache::forget($token);
        $user = User::find($userId);
        if (!isset($user)) {
            return '用户不存在';
        }
        $user->openid = $request->openid;
        $user->save();
        return '绑定成功,请到APP里确认';
    }
}
